<?php
namespace Admin\Models\stats;

use Admin\Models\BaseModel;

class XhstatsExamine extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iSEId;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iSchoolId;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     * @explain 考核模式，1=全校，2=年级
     */
    public $iMode;

    /**
     *
     * @var string
     * @Column(type="string", length=50, nullable=true)
     */
    public $sTargetMonth;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iStartTime;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iEndTime;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mStatsInfo;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mTchInfo;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mIgnore;

    /**
     *
     * @var string|array
     * @Column(type="string", nullable=true)
     */
    public $mScoreInfo;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iScore;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mDevInfo;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bLock;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHStats_Examine';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhstatsExamine[]|XhstatsExamine
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhstatsExamine
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 数据预处理
     *
     * @return bool
     *
     * create by ck 20171030
     */
    public function afterFetch()
    {
        $this->mStatsInfo = json_decode(gzuncompress(base64_decode($this->mStatsInfo)), true);
        $this->mDevInfo   = json_decode(gzuncompress(base64_decode($this->mDevInfo)), true);
        $this->mTchInfo   = json_decode(gzuncompress(base64_decode($this->mTchInfo)), true);
        $this->mIgnore    = json_decode(gzuncompress(base64_decode($this->mIgnore)), true);
        $this->mScoreInfo = $this->mScoreInfo ? unserialize($this->mScoreInfo) : [];
        return true;
    }

    /**
     * 保存前处理数据
     *
     * @return bool
     *
     * create by ck 20171030
     */
    public function beforeSave()
    {
        $this->mStatsInfo = base64_encode(gzcompress(json_encode($this->mStatsInfo, JSON_UNESCAPED_UNICODE)));
        $this->mDevInfo   = base64_encode(gzcompress(json_encode($this->mDevInfo, JSON_UNESCAPED_UNICODE)));
        $this->mTchInfo   = base64_encode(gzcompress(json_encode($this->mTchInfo, JSON_UNESCAPED_UNICODE)));
        $this->mIgnore    = base64_encode(gzcompress(json_encode($this->mIgnore, JSON_UNESCAPED_UNICODE)));
        foreach ($this->mScoreInfo as &$row) {
            if (is_array($row)) {
                foreach ($row as &$score) {
                    $score = strval($score);
                }
            } else {
                $row = strval($row);
            }
        }
        unset($score);
        $this->mScoreInfo = serialize($this->mScoreInfo);
        return true;
    }

    /**
     * 根据学校id，目标月份获取应用得分
     *
     * @param $schoolId
     * @param $targetMonth
     * @return array|string
     * @throws \Exception
     *
     * create by ck 20180423
     */
    public static function getScoreInfo($schoolId, $targetMonth)
    {
        $obj = self::findFirst([
            'columns'    => 'mScoreInfo,iMode,iScore',
            'conditions' => 'bDelete = 0 AND iSchoolId = :schoolId: AND sTargetMonth = :month:',
            'bind'       => ['schoolId' => $schoolId, 'month' => $targetMonth]
        ]);
        if (!$obj) {
            throw new \Exception('该学校数据未生成');
        }
        if (!$obj->mScoreInfo) {
            throw new \Exception('应用分数未生成');
        }
        $mScoreInfo = is_array($obj->mScoreInfo) ? $obj->mScoreInfo : unserialize($obj->mScoreInfo);
        return ['mScoreInfo' => $mScoreInfo,'iMode' => $obj->iMode, 'finalScore' => $obj->iScore];
    }

    /**
     * 获取学校考核信息
     *
     * @param $schoolId
     * @param $targetMonth
     * @return int
     *
     * create by sqb 20190118
     */
    public static function getExamineInfo($schoolId, $targetMonth)
    {
        $obj = self::findFirst([
            'conditions' => 'bDelete = 0 AND iSchoolId = :schoolId: AND sTargetMonth = :month:',
            'bind'       => ['schoolId' => $schoolId, 'month' => $targetMonth]
        ]);

        if (!$obj) {
            return 0;
        }

        return $obj->toArray();
    }
}
